<?php
/**
 * Created by PhpStorm.
 * User: hhlov_000
 * Date: 15-2-7
 * Time: 下午11:10
 */

namespace Admin\Controller;


class ReportController extends CommonController {

    public function reportList($page = 1, $rows = 10, $sort = 'time', $order = 'desc'){
        if(IS_POST){
            $report_db = M('report');
            $total = $report_db->count();
            $order = $sort.' '.$order;
            $limit = ($page - 1) * $rows . "," . $rows;
            //$list = $member_db->table(C('DB_PREFIX').'user M')->join(C('DB_PREFIX').'member_type MT on MT.typeid = M.typeid')->field("M.memberid,M.user_name,M.sex")->order($order)->limit($limit)->select();
            //$list = $member_db->field('user_id,user_name,sex,phone,status')->order($order)->limit($limit)->select();
            $list = $report_db->order($order)->limit($limit)->select();
            if(!$list) $list = array();
            $data = array('total'=>$total, 'rows'=>$list);
            $this->ajaxReturn($data);
        }else{
            $menu_db = D('Menu');
            $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
            $datagrid = array(
                'options'     => array(
                    'title'   => $currentpos,
                    'url'     => U('Report/reportList', array('grid'=>'datagrid')),
                    'toolbar' => 'report_reportlist_datagrid_toolbar',
                ),
                'fields' => array(
                    '被举报人'      => array('field'=>'reported_id','width'=>15,'sortable'=>true,'formatter'=>'nameFormatter'),
                    '举报信息'    => array('field'=>'content','width'=>35,'sortable'=>false),
                    '举报时间'    => array('field'=>'time','width'=>20,'sortable'=>true),
                    '举报人'    => array('field'=>'reporter_id','width'=>15,'sortable'=>true,'formatter'=>'nameFormatter'),
                    '是否处理' => array('field'=>'is_deal','width'=>8,'sortable'=>true,'formatter'=>'sugFormatter'),
                    '处理意见' => array('field'=>'feedback','width'=>35,'sortable'=>true),
                    '管理操作'    => array('field'=>'report_id','width'=>15,'formatter'=>'reportListOperateFormatter'),
                )
            );
            $this->assign('datagrid', $datagrid);
            $this->display('report_list');
        }

    }

    public function getNameById($id=0){

        if($id==0){

            echo '查无此人';
        }else{
            $user_db = D('user');
            $result = $user_db->where("user_id=%d",$id)->field(array('user_name'))->select();
            if(sizeof($result)==1){
                echo $result[0]['user_name'];
            }else{

                echo '查无此人';
            }
        }
    }

    public function reportDelete($report_id){
        $report_db = M('report');
        $result = $report_db->where(array('report_id'=>$report_id))->delete();
        if ($result){
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

    public function reportDeal($report_id){
        if(IS_POST){
            $data['feedback'] = $_POST['content'];
            $data['is_deal'] = 1;
            $report_db = M('report');
            $report_db->where("report_id=%d",$report_id)->save($data);
            echo '提交成功';



        }else{
            $report_db = M('report');
            $report_list = $report_db->where("report_id=%d",$report_id)->select();
            if(sizeof($report_list)==1){
                $this->assign('report',$report_list[0]);
                $this->assign('reporter_name',$this->getUserInfoById($report_list[0]['reporter_id']));
                $this->assign('reported_name',$this->getUserInfoById($report_list[0]['reported_id']));
                $this->display('report_deal');
            }else{
                    echo '查询失败';
            }

        }
    }

    private function getUserInfoById($id=0){

        if($id==0){

            return '查无此人';
        }else{
            $user_db = D('user');
            $result = $user_db->where("user_id=%d",$id)->field(array('user_name'))->select();
            if(sizeof($result)==1){
                return $result[0]['user_name'];
            }else{

                return '查无此人';
            }
        }
    }

}